package com.yanqu.road.dao.impl.unioncampwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.unioncampwar.UnionCampWarCampData;
import com.yanqu.road.entity.unioncampwar.UnionCampWarUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionCampWarUserDaoImpl extends TempDataObjectDao<UnionCampWarUserData> {


    public static String getUnionCampWarUserDataSql = "select * from `t_c_union_camp_war_user` where `weekId`=?";

    public Map<Integer, Map<Integer, List<UnionCampWarUserData>>> getUnionCampWarUserDataMap(int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, List<UnionCampWarUserData>>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getUnionCampWarUserDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionCampWarUserData data = parse(rs);
                Map<Integer, List<UnionCampWarUserData>> centerGroupMap = dataMap.get(data.getCenterGroupId());
                if (centerGroupMap == null) {
                    centerGroupMap = new ConcurrentHashMap<>();
                    dataMap.put(data.getCenterGroupId(), centerGroupMap);
                }
                List<UnionCampWarUserData> userDataList = centerGroupMap.get(data.getGroupId());
                if (userDataList == null) {
                    userDataList = new ArrayList<>();
                    centerGroupMap.put(data.getGroupId(), userDataList);
                }
                userDataList.add(data);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUnionCampWarUserDataSql = "update `t_c_union_camp_war_user` set campId=?,serverId=?,score=?,attackDamageMap=?,rank=?,getReward=? where centerGroupId=? and groupId=? and weekId=? and userId=?";

    @Override
    protected boolean updateData(UnionCampWarUserData unionCampWarUserData) {
        unionCampWarUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarUserData.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarUserData.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarUserData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionCampWarUserData.getAttackDamageMap())));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarUserData.getRank()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarUserData.getGetReward()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarUserData.getCenterGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarUserData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarUserData.getUserId()));
        if (getDb().executeNoneQuery(updateUnionCampWarUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUnionCampWarUserDataSql = "insert into `t_c_union_camp_war_user`(centerGroupId,groupId,weekId,campId,userId,serverId,score,attackDamageMap,rank,getReward) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UnionCampWarUserData unionCampWarUserData) {
        unionCampWarUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarUserData.getCenterGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarUserData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarUserData.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarUserData.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarUserData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionCampWarUserData.getAttackDamageMap())));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarUserData.getRank()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarUserData.getGetReward()));
        if (getDb().executeNoneQuery(insertUnionCampWarUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UnionCampWarUserData parseData(ResultSet rs) throws SQLException {
        UnionCampWarUserData unionCampWarUserData = new UnionCampWarUserData();
        unionCampWarUserData.setCenterGroupId(rs.getInt("centerGroupId"));
        unionCampWarUserData.setGroupId(rs.getInt("groupId"));
        unionCampWarUserData.setWeekId(rs.getInt("weekId"));
        unionCampWarUserData.setCampId(rs.getInt("campId"));
        unionCampWarUserData.setUserId(rs.getLong("userId"));
        unionCampWarUserData.setServerId(rs.getLong("serverId"));
        unionCampWarUserData.setScore(rs.getInt("score"));
        unionCampWarUserData.setAttackDamageMap(JSON.parseObject(rs.getString("attackDamageMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {
        }));
        unionCampWarUserData.setRank(rs.getInt("rank"));
        unionCampWarUserData.setGetReward(rs.getInt("getReward"));

        return unionCampWarUserData;
    }
}
